<?php
class PinkMember
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 20;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'reportlist','searchList','reportedit'
        );
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    /**
     * 举报列表
     * @param $req
     * @return string
     * @throws Exception
     */
    private function reportlist($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $pageUrl = $this->pageUrl($req);
        return render('report_list', array('list' => $res , 'pageUrl' => $pageUrl));
    }

    function reportedit($req){
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    /**
     * 搜索
     * @param unknown $req
     * @return unknown
     */
    private function searchList ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $pageUrl = $this->pageUrl($req);
        return render('report_list', array('list' => $res , 'pageUrl' => $pageUrl));
    }

    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'reportlist':
                $param = array(
                    'do'     => 'postReport',
                    'format' => 'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
            case 'reportedit':
                $param = array(
                    'do'     => 'updateReport',
                    'format' => 'json',
                    'status'  => isset($req['status']) ? ( in_array($req['status'], [0,1,2]) ? $req['status'] : 0 ) : 0,
                    'muid'   => intval($req['muid']),
                    'ruid'   => intval($req['ruid']),
                    'type'   => intval($req['type']),
                    'id'     => isset($req['id']) ? intval($req['id']) : 0 ,
                    'action'     => !empty($req['action']) ? urlencode($req['action']) : '' ,
                    'dealResult'   => intval($req['status'])
                );
                break;
            case 'searchList':
                $param = array(
                    'do'     => 'postReport',
                    'format' => 'json',
                    'reasonType'=> isset($req['reasonType']) ? $req['reasonType'] : '',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
        }

        return $param;
    }

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "reportlist":
                $mpurl .= "&id={$req['id']}"."&reasonType={$req['reasonType']}";
                break;
            case "searchList":
                $mpurl .= "&id={$req['id']}"."&reasonType={$req['reasonType']}";
                break;
        }
        $pageurl = multi($req['start'], $this->length, $mpurl);
        return $pageurl;
    }

}

$req = request();
$pinkMember   = new PinkMember($_SC ['apiurl']);
echo $pinkMember->execute($req['op']);





